<template>


  <div class="login-wrap">
    <!-- <el-dialog
  title="提示"
  :visible.sync="dialogVisible"
  width="30%"
  :before-close="handleClose">
  <span>这是一段信息</span> -->
  <el-form label-position="left" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm login-container">
      <h3 class="title">用户登录</h3>
      <el-form-item prop="userName">
        <el-input type="text" v-model="ruleForm.userName" auto-complete="off" placeholder="账号"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="ruleForm.password" auto-complete="off" placeholder="密码"></el-input>
      </el-form-item>
      
      <el-checkbox class="remember" v-model="rememberpwd">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button class="button1" type="primary" style="width:100%;"   @click="login()" :loading="logining">用户登录</el-button>
        <el-button  class="button2" type="primary" style="width:100%;" @click="dl()" :loading="logining1">审核者登录</el-button>
         <el-button  class="button3" type="primary" style="width:100%;" @click="gohome()" :loading="logining">首页</el-button>
      </el-form-item>
    </el-form>



<!-- </el-dialog> -->
  
  </div>
</template>
<script type="text/ecmascript-6">
import axios from 'axios';
  
import { setCookie, getCookie, delCookie } from '../utils/util'
import md5 from 'js-md5'

export default {
  name: 'login',
  data() {
    return {
      //定义loading默认为false
      logining: false,
      // 记住密码
      rememberpwd: false,
      logining1: false,
      ruleForm: {
        //username和password默认为空
        userName: '',
        password: '',
        code: '',
        randomStr: '',
        codeimg: ''
      },
      //rules前端验证
      rules: {
        username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      }
    }
  },
  // 创建完毕状态(里面是操作)
  created() {
  
 
    // 获取存在本地的用户名密码
    this.getuserpwd()
    
  },
  // 里面的函数只有调用才会执行
  methods: {
    gohome(){
      this.$router.push({ path: "/" });

    },
    // 获取用户名密码
    getuserpwd() {
      if (getCookie('user') != '' && getCookie('pwd') != '') {
        this.ruleForm.userName = getCookie('user')
        this.ruleForm.password = getCookie('pwd')
        this.rememberpwd = true
      }
    },

 dl(){
       axios.post("http://47.106.193.50:8080/adminLogin",this.ruleForm).then(res=>{
          let code = res.data.code
          console.log(code);
          if (code == "200") {
          
              setTimeout(() => {
                this.logining = false
                this.$store.commit('login', 'true')
                // 缓存token
                // localStorage.setItem('user', JSON.stringify(res.data.data))
                // localStorage.setItem('data', res.data.data)
                // 缓存用户个人信息
                this.$store.commit("savauser", JSON.stringify(res.data.data));
                // localStorage.setItem("userdata", JSON.stringify(this.pickerItem));
                // computed 中调用
                // this.$store.state.xxx  调用
                 localStorage.setItem('user', JSON.stringify(res.data.data))
                localStorage.setItem('role', res.data.data.role)
                    let name = localStorage.getItem("user")
                    localStorage.setItem('id', res.data.data.id)
                  console.log(name,"登录信息"); 
                 this.$router.push({
                   path: '/pay/szdxx',
                
                     }) 
                       this.$message({
              message: '成功',
              type: 'success'
            })
     
              }, 1000)
            } else {
              this.$message.error(res.data.msg)
              this.logining = false
       
            }
        
          }
       )

    },


    login(){
       axios.post("http://47.106.193.50:8080/userLogin",this.ruleForm).then(res=>{
          let code = res.data.code
          if (code == "200") {
              setTimeout(() => {
                this.logining = false
            this.$store.commit('login', 'true')

                // 缓存token
                localStorage.setItem('user', JSON.stringify(res.data.data))
                localStorage.setItem('role', res.data.data.role)
                localStorage.setItem('userName', res.data.data.userName)
                localStorage.setItem('phone', res.data.data.phone)
                localStorage.setItem('email', res.data.data.email)
                localStorage.setItem('realName', res.data.data.realName)
                localStorage.setItem('sex', res.data.data.sex)
                localStorage.setItem('createDate', res.data.data.createDate)
                localStorage.setItem('id', res.data.data.id)
                localStorage.setItem('password', res.data.data.password)
               
         
                // 缓存用户个人信息
            this.$store.commit("savauser", res.data.data);
            // localStorage.setItem("userdata", JSON.stringify(this.pickerItem));
            // computed 中调用
            // this.$store.state.xxx  调用
                 let name = localStorage.getItem("role")
                 let xx = localStorage.getItem("user")

        console.log(name,"登录信息");
        if(name == "捐助者"){
             this.$router.push({
                   path: '/pay/szdxx',
                   
                 })
      }else if(name == "受助者"){
         this.$router.push({
                   path: '/pay/szdxx',
                   
                 }) }

        else{
            this.$router.push('/')
        }
              }, 1000)
            } else {
              this.$message.error(res.data.msg)
              this.logining = false
       
            }
        
          }
       )

    },
    //获取info列表
   
  }
}
</script>

<style scoped>
.button1{
 
}
.el-button+.el-button {
     margin-left: 0px; 
}
.button2{
  margin-top:10px;
 
}
.button3{
  margin-top:10px;
 
}
.login-wrap {
  box-sizing: border-box;
  /* width: 50%; */
  /* height: 50%; */
  padding-top: 10%;
  background-image: url();
  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}
.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}
.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
.remember {
  margin: 0px 0px 35px 0px;
}
.code-box {
  text-align: right;
}
.codeimg {
  height: 40px;
}
</style>